package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;

import com.yellowpage.bigdata.dao.TmApponoffActiveMonthDao;
import com.yellowpage.bigdata.data.TmApponoffActiveMonth;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class TmApponoffActiveMonthServiceImpl extends BaseServiceImpl implements
		TmApponoffActiveMonthService {
	private TmApponoffActiveMonthDao tmApponoffActiveMonthDao;

	public void setTmApponoffActiveMonthDao(
			TmApponoffActiveMonthDao tmApponoffActiveMonthDao) {
		this.tmApponoffActiveMonthDao = tmApponoffActiveMonthDao;
	}

	/* Smooth generate Method */
	public TmApponoffActiveMonth getTmApponoffActiveMonth(Long id) {
		return tmApponoffActiveMonthDao.get(id);
	}

	public List<TmApponoffActiveMonth> getAllTmApponoffActiveMonth() {
		return tmApponoffActiveMonthDao.getAll();
	}

	public List<Map> queryTmApponoffActiveMonthByPage(Map queryMap,
			PageRender pageRender) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT a.CNTS , a.MONTH_ID , a.VERSION , a.CHANNEL_NO ,(SELECT d.NAME FROM OMC_DIC_TABLE d where d.VALUE = a.CHANNEL_NO and d.DIC_NAME='NSP_ONOFFLOG_CHANNEL_NO') as CHANNEL_NO_NAME,ROUND(b.RATE*100 ,2)RATE, ROUND(a.CNTS*100/c.sam,2) PROPORTION FROM TM_APPONOFF_ACTIVE_MONTH a,TM_APPONOFF_ACTIVE_RATE_MONTH b,(SELECT sum(CNTS) sam from TM_APPONOFF_ACTIVE_MONTH c1 ,TM_APPONOFF_ACTIVE_RATE_MONTH c2 WHERE c1.MONTH_ID =c2.Month_ID and c1.CHANNEL_NO =c2.CHANNEL_NO and c1.VERSION = c2.VERSION ");
		if(!"".equals(queryMap.get("monthId_start"))){
			sql.append(" AND c1.MONTH_ID >= " + queryMap.get("monthId_start"));
		}
		if(!"".equals(queryMap.get("monthId_end"))){
			sql.append(" AND c1.MONTH_ID <= " + queryMap.get("monthId_end"));
		}
		if(!"".equals(queryMap.get("channelNo"))){
			sql.append(" AND c1.CHANNEL_NO = '" + queryMap.get("channelNo") + "'");
		}
		if(!"".equals(queryMap.get("version"))){
			sql.append(" AND c1.VERSION = '" + queryMap.get("version") + "'");
		}
		sql.append(" )c WHERE a.MONTH_ID =b.Month_ID and a.CHANNEL_NO =b.CHANNEL_NO and a.VERSION = b.VERSION");
		if(!"".equals(queryMap.get("monthId_start"))){
			sql.append(" AND a.MONTH_ID >= " + queryMap.get("monthId_start"));
		}
		if(!"".equals(queryMap.get("monthId_end"))){
			sql.append(" AND a.MONTH_ID <= " + queryMap.get("monthId_end"));
		}
		if(!"".equals(queryMap.get("channelNo"))){
			sql.append(" AND a.CHANNEL_NO = '" + queryMap.get("channelNo") + "'");
		}
		if(!"".equals(queryMap.get("version"))){
			sql.append(" AND a.VERSION = '" + queryMap.get("version") + "'");
		}
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(sql.toString());

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveTmApponoffActiveMonth(
			TmApponoffActiveMonth tmApponoffActiveMonth) {
		tmApponoffActiveMonthDao.save(tmApponoffActiveMonth);
	}

	public void updateTmApponoffActiveMonth(
			TmApponoffActiveMonth tmApponoffActiveMonth) {
		tmApponoffActiveMonthDao.update(tmApponoffActiveMonth);
	}

	public void delete(List deleteList) {
		tmApponoffActiveMonthDao.remove(deleteList);
	}

}